﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.CodeGenerator
{
    public class ConstraintCollection : System.Collections.ObjectModel.KeyedCollection<string, Constraint>
    {
        #region Public Members

        #region Add

        public void Add(string name, IEnumerable<DomainObjectProperty> properties)
        {
            var item = new Constraint()
            {
                Name = name,
            };
            item.Properties.AddRange(properties);
            Add(item);
        }
        public void Add(string name, IEnumerable<string> propertyNames)
        {
            var item = new Constraint()
            {
                Name = name,
            };
            item.Properties.AddRange(propertyNames);
            Add(item);
        }
        public void Add(string name, params string[] propertyNames)
        {
            Add(name, propertyNames.AsEnumerable());
        }

        #endregion
        #region FindByName

        public Constraint FindByName(string name)
        {
            if (Contains(name))
                return this[name];
            return null;
        }

        #endregion
        #region GetKeyForItem

        protected override string GetKeyForItem(Constraint item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");
            return item.Name;
        }

        #endregion
        #region Import

        public void Import(IEnumerable<Constraint> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
            {
                if (Contains(item.Name))
                    this[item.Name].Import(item);
                else
                    Add(item);
            }
        }

        #endregion

        #endregion
        #region Constructors

        public ConstraintCollection()
            : base(System.StringComparer.OrdinalIgnoreCase)
        {
        }

        #endregion
    }
}
